package com.wali.modules.usr.control;

import com.wali.common.ccp.exception.CcpException;
import com.wali.common.ccp.page.CcpPagination;
import com.wali.common.enums.ErrorCodeEnum;
import com.wali.common.web.BaseController;
import com.wali.modules.system.enums.RoleTypeEnum;
import com.wali.modules.system.vo.SysUserVO;
import com.wali.modules.usr.bo.UsrReceiverBO;
import com.wali.modules.usr.service.UsrReceiverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author walixu
 * @ClassName com.wali.modules.usr.control
 * @Description 发件人controller类
 * @email wali.xu@kpmg.com
 * @date 26/01/2019 8:56 下午
 */
@Controller
@RequestMapping(value = "/usr")
public class UsrReceiverController extends BaseController {

    @Autowired
    private UsrReceiverService usrReceiverService;

    /**
     * @param
     * @return
     * @throws
     * @Title listUsrReceiver
     * @Description 检索查询指定用户下的收件人列表
     */
    @RequestMapping(value = "/listUsrReceiver")
    public ResponseEntity<String> listUsrReceiver(HttpServletRequest request, UsrReceiverBO usrReceiver, @RequestParam("page") int page, @RequestParam("limit") int limit) {
        int code = ErrorCodeEnum.ERROR_INTERNAL_CODE.getCode();
        String msg = ErrorCodeEnum.ERROR_INTERNAL_CODE.getMessage();

        try {
            logger.info(usrReceiver.toString());

            HttpSession session = request.getSession(false);
            SysUserVO sysUser = (SysUserVO) session.getAttribute("user");

            if (sysUser.getRoleType() == RoleTypeEnum.NORMAL_USER_TYPE.getType()) {
                usrReceiver.setUserId(sysUser.getId());
            } else if (sysUser.getRoleType() == RoleTypeEnum.STORE_ADMIN_USER_TYPE.getType()) {
                usrReceiver.setId(-1);// -1表示分店管理员查询收件人特殊标志，代码只能查询其自己的收件人
            }

            CcpPagination usrReceiverList = usrReceiverService.listUsrReceiver(usrReceiver, page, limit, sysUser.getStoreId());
            return getResponseEntity(usrReceiverList);
        } catch (CcpException e) {
            logger.error(e.getMessage(), e);
            code = e.getException().getCode();
            msg = e.getException().getMessage();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        return getResponseEntity(code, msg);
    }

    /**
     * @param
     * @return
     * @throws
     * @Title deleteReceiver
     * @Description 删除收件人信息
     */
    @RequestMapping(value = "/deleteUsrReceiver")
    public ResponseEntity<String> deleteUsrReceiver(UsrReceiverBO usrReceiver) {
        int code = ErrorCodeEnum.ERROR_INTERNAL_CODE.getCode();
        String msg = ErrorCodeEnum.ERROR_INTERNAL_CODE.getMessage();

        try {
            logger.info(usrReceiver.toString());

            int count = usrReceiverService.deleteUsrReceiver(usrReceiver);
            if (count > 0) {
                code = ErrorCodeEnum.SUCCESS_CODE.getCode();
                msg = ErrorCodeEnum.SUCCESS_CODE.getMessage();
            }
            return getResponseEntity(code, msg);
        } catch (CcpException e) {
            logger.error(e.getMessage(), e);
            code = e.getException().getCode();
            msg = e.getException().getMessage();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        return getResponseEntity(code, msg);
    }
}
